package cn.rkylin.oms.ext.cainiao.item.controller;

import cn.rkylin.core.controller.ApolloController;
import cn.rkylin.oms.common.export.IExport;
import cn.rkylin.oms.common.utils.LogUtil;
import cn.rkylin.oms.ext.cainiao.item.dao.CainiaoItemDAO;
import cn.rkylin.oms.ext.cainiao.item.vo.CainiaoItemVO;
import cn.rkylin.oms.ext.cainiao.item.service.ICainiaoItemService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * 菜鸟商品查询控制类
 *
 * @author zhangheng
 * @date 2018/04/03
 */
@Controller
@RequestMapping("/cainiaoitem")
public class CainiaoItemController extends ApolloController {
    private static final Log logger = LogFactory.getLog(CainiaoItemController.class);

    @Autowired
    public ICainiaoItemService cainiaoItemService;

    @Autowired
    private LogUtil logUtil;

    /**
     * getcaiNiaoItemInfo:根据查询条件获取菜鸟商品信息列表. <br/>
     *
     * @param quickSearch
     * @param start
     * @param length
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/cainiaogetItemList")
    public Map<String, Object> getcaiNiaoItemList(String quickSearch, String formJson, @RequestParam(required = false, defaultValue = "0") int start,
                                            @RequestParam(required = false, defaultValue = "10") int length ) throws Exception {
        logger.info("查询菜鸟商品信息：/cainiaoitem/cainiaogetItemList");
        Map<String, Object> returnMap = new HashMap<>();

        CainiaoItemVO queryParamVO = new CainiaoItemVO();
        // 高级查询检索条件
        formJson = URLDecoder.decode(formJson, "UTF-8");
        queryParamVO = JSONObject.parseObject(formJson, CainiaoItemVO.class);
        // 快速查询
        if (quickSearch != null && StringUtils.isNotEmpty(quickSearch)) {
            quickSearch = URLDecoder.decode(quickSearch, "UTF-8");
            if (StringUtils.isNotEmpty(quickSearch)) {
                queryParamVO.setQuickSearch(quickSearch);
            }
        }

        Map<String, String> replaceFieldsMap = new HashMap<String, String>();
        // 排序语句生成
        String orderStatement = getOrderString(CainiaoItemDAO.class.getName(), "CainiaopageSelectItem", replaceFieldsMap);
        if (StringUtils.isNotEmpty(orderStatement)) {
            queryParamVO.setOrderBy(orderStatement);
        }
        // 处理分页信息
        if (length == -1) {
            length = Integer.MAX_VALUE;
        }
        int page = start / length + 1;

        // 执行查询
        PageInfo<CainiaoItemVO> itemVOList = cainiaoItemService.queryItemList(page, length, queryParamVO);

        if (null != itemVOList) {
            returnMap = getSuccessMap("查询菜鸟商品数据成功");
            returnMap.put(RECORDS_FILTERED, itemVOList.getTotal());
            returnMap.put(RECORDS_TOTAL, itemVOList.getTotal());
            returnMap.put(RETURN_DATA, itemVOList.getList());
        } else {
            returnMap = getFailedMap("查询菜鸟商品数据失败");
        }
        logger.info("查询菜鸟商品数据调用结束");
        return returnMap;
    }



    private Map<String, Object> getSuccessMap(String message) {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        returnMap.put(JSON_RESULT, SUCCESS);
        returnMap.put(JSON_MSG, message);
        return returnMap;
    };

    private Map<String, Object> getFailedMap(String message) {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        returnMap.put(JSON_RESULT, FAILED);
        returnMap.put(JSON_MSG, message);
        return returnMap;
    };


}